<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\cache\driver\Redis;
use app\api\service\MatchingOrderService;

class MatchingOrder extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * 更新管家坐标
     *
     */
    public function update_coordinate(){
        $data = $this->request->post();

        // 生成缓存的键名
        $redis = new Redis();
        $cacheKey = 'submit_' . md5(serialize($data));

        // 检查缓存中是否存在该键名
        if ($redis->has($cacheKey)) {
            // 表单已经被重复提交
            return json(['code'=>0,'msg'=>'操作频繁','data'=>'']);
        }
        // 将键名存储到缓存中，设置有效期为1秒
        $redis->set($cacheKey, true, 1);

        $res = MatchingOrderService::updateCoordinates($data);
        return json($res);
    }

    /**
     * 服务时间
     *
     */
    public function service_time(){
        $data = $this->request->post();
        $res = MatchingOrderService::getServiceTime($data);
        return json($res);
    }

    /**
     * 设置服务时间
     *
     */
    public function set_service_time(){
        $data = $this->request->post();
        $res = MatchingOrderService::setServiceTime($data);
        return json($res);
    }

    /**
     * 管家端首页订单列表
     *
     */
    public function match_order_lists(){
        $data = $this->request->post();
        $res = MatchingOrderService::matchingOrderList($data);
        return json($res);
    }

    /**
     * 管家端抢单
     *
     */
    public function order_grabbing(){
        $data = $this->request->post();

        // 生成缓存的键名
        $redis = new Redis();
        $cacheKey = 'submit_' . md5(serialize($data));

        // 检查缓存中是否存在该键名
        if ($redis->has($cacheKey)) {
            // 表单已经被重复提交
            return json(['code'=>0,'msg'=>'操作频繁','data'=>'']);
        }
        // 将键名存储到缓存中，设置有效期为1秒
        $redis->set($cacheKey, true, 1);

        $res = MatchingOrderService::orderGrabbing($data);
        return json($res);
    }

    /**
     * 管家出发
     *
     */
    public function order_departure(){
        $data = $this->request->post();
        $res = MatchingOrderService::orderDeparture($data);
        return json($res);
    }

    /**
     * 管家到达
     *
     */
    public function order_arrive(){
        $data = $this->request->post();
        $res = MatchingOrderService::orderArrive($data);
        return json($res);
    }

    /**
     * 学生拍照
     *
     */
    public function student_photo(){
        $data = $this->request->post();
        $res = MatchingOrderService::studentPhoto($data);
        return json($res);
    }

    /**
     * 管家订单列表
     *
     */
    public function steward_order_list(){
        $data = $this->request->post();
        $res = MatchingOrderService::stewardOrderList($data);
        return json($res);
    }

    /**
     * 管家订单详情
     *
     */
    public function steward_order_detail()
    {
        $data = $this->request->post();
        $res = MatchingOrderService::stewardOrderDetail($data);
        return json($res);
    }

}